import 'package:flui/flui.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:fzcgx_app/common/adapter_utils.dart';
import 'package:fzcgx_app/common/assets_utils.dart';
import 'package:fzcgx_app/network/http.dart';
import 'package:fzcgx_app/ui/my/address/model/address_model.dart';
import 'package:fzcgx_app/ui/widget/custom_appbar_widget.dart';
import 'package:fzcgx_app/ui/widget/my_separator_widget.dart';
import 'package:fzcgx_app/ui/widget/select_address_widget.dart';

class AddressEditPage extends StatefulWidget {
  AddressEditPage({Key key, this.model, this.callBack}) : super(key: key);
  final Function callBack;
  final AddressModel model;
  @override
  _AddressEditPageState createState() => _AddressEditPageState();
}

class _AddressEditPageState extends State<AddressEditPage> {
  bool isActive = false;
  TextEditingController acceptNameController = TextEditingController();
  TextEditingController phoneController = TextEditingController();
  TextEditingController addressController = TextEditingController();
  String cityID = '';
  String cityStr = '';

  @override
  void initState() {
    super.initState();
    acceptNameController.text = widget.model.acceptName;
    phoneController.text = widget.model.phone;
    addressController.text = widget.model.address;
    cityID = widget.model.city;
    cityStr = widget.model.cityName;
    isActive = widget.model.check == 1 ? true : false;
  }

  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Color(0xff141F33),
        appBar: CustomAppbar(
          backgroundColor: Color(0xff141F33),
          title: '编辑地址',
          themeColor: Colors.white,
          actions: <Widget>[
            InkWell(
                onTap: bindDelet,
                child: Container(
                  alignment: Alignment.center,
                  padding: EdgeInsets.only(right: AdapterUtils.getWidth(15)),
                  child:
                      Image.asset(AssetsUtils.getAssetsImagesPath('del.png')),
                ))
          ],
        ),
        body: SafeArea(child: _buildBodyWidget()),
        bottomNavigationBar: InkWell(
          onTap: () {
            this.postApi();
          },
          child: Container(
            padding: EdgeInsets.all(AdapterUtils.getWidth(15)),
            color: Color(0xff141F33),
            height: AdapterUtils.getHeight(80),
            child: Container(
              alignment: Alignment.center,
              decoration: BoxDecoration(
                color: Color(0xffFF3D3E),
                borderRadius: BorderRadius.all(
                  Radius.circular(26),
                ),
              ),
              child: Text('确认保存',
                  style: TextStyle(
                      color: Colors.white,
                      fontWeight: FontWeight.bold,
                      fontSize: AdapterUtils.getSp(18))),
            ),
          ),
        ));
  }

  Widget _buildBodyWidget() {
    return SingleChildScrollView(
      child: Container(
        padding: EdgeInsets.symmetric(
          horizontal: AdapterUtils.getWidth(15),
        ),
        child: Column(
          children: <Widget>[
            Container(
              child: Row(
                children: <Widget>[
                  Text('收货人名',
                      style: TextStyle(
                          color: Color(0xff99A2AD),
                          fontWeight: FontWeight.w500,
                          fontSize: AdapterUtils.getSp(14))),
                  SizedBox(
                    width: AdapterUtils.getWidth(33),
                  ),
                  Expanded(
                    child: TextField(
                      controller: acceptNameController,
                      // keyboardType: TextInputType.phone,
                      style: TextStyle(
                        fontSize: AdapterUtils.getSp(14),
                        color: Colors.white,
                        fontWeight: FontWeight.w500,
                      ),
                      decoration: InputDecoration(
                          hintText: '收货人',
                          border: InputBorder.none,
                          hintStyle: TextStyle(
                            fontSize: AdapterUtils.getSp(14),
                            color: Color(0xff606B79),
                            fontWeight: FontWeight.w500,
                          )),
                    ),
                  ),
                ],
              ),
            ),
            Container(
              child: Row(
                children: <Widget>[
                  Text('手机号码',
                      style: TextStyle(
                          color: Color(0xff99A2AD),
                          fontWeight: FontWeight.w500,
                          fontSize: AdapterUtils.getSp(14))),
                  SizedBox(
                    width: AdapterUtils.getWidth(33),
                  ),
                  Expanded(
                    child: TextField(
                      controller: phoneController,
                      keyboardType: TextInputType.phone,
                      style: TextStyle(
                        fontSize: AdapterUtils.getSp(14),
                        color: Colors.white,
                        fontWeight: FontWeight.w500,
                      ),
                      decoration: InputDecoration(
                          hintText: '手机号码',
                          border: InputBorder.none,
                          hintStyle: TextStyle(
                            fontSize: AdapterUtils.getSp(14),
                            color: Color(0xff606B79),
                            fontWeight: FontWeight.w500,
                          )),
                    ),
                  ),
                ],
              ),
            ),
            InkWell(
              onTap: () {
                showModalBottomSheet(
                  context: context,

                  ///_handleLocal是被回调页面的方法 用于接收选择的地址
                  builder: (context) => SelectAddressWidget(
                    valueCb: (selectMap, localStr) {
                      if (mounted) {
                        setState(() {
                          this.cityID = selectMap['selectDistrictId'];
                          this.cityStr = localStr;
                        });
                      }
                    },
                  ),
                );
              },
              child: Container(
                height: AdapterUtils.getHeight(50),
                child: Row(
                  children: <Widget>[
                    Text('所在地区',
                        style: TextStyle(
                            color: Color(0xff99A2AD),
                            fontWeight: FontWeight.w500,
                            fontSize: AdapterUtils.getSp(14))),
                    SizedBox(
                      width: AdapterUtils.getWidth(33),
                    ),
                    Text(this.cityStr.isEmpty ? '请选择' : this.cityStr,
                        style: TextStyle(
                            color: this.cityStr.isEmpty
                                ? Color(0xff606B79)
                                : Colors.white,
                            fontWeight: FontWeight.w500,
                            fontSize: AdapterUtils.getSp(14))),
                    Spacer(),
                    Image.asset(
                        AssetsUtils.getAssetsImagesPath('arrow_right.png'))
                  ],
                ),
              ),
            ),
            Container(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  Text('详细地址',
                      style: TextStyle(
                          color: Color(0xff99A2AD),
                          fontWeight: FontWeight.w500,
                          fontSize: AdapterUtils.getSp(14))),
                  TextField(
                    controller: addressController,
                    // keyboardType: TextInputType.phone,

                    textInputAction: TextInputAction.done,
                    maxLines: 2,
                    minLines: 1,
                    style: TextStyle(
                      fontSize: AdapterUtils.getSp(14),
                      color: Colors.white,
                      fontWeight: FontWeight.w500,
                    ),
                    decoration: InputDecoration(
                        hintText: '请输入详细地址信息，如道路、门牌号、写字楼座号、单元室等',
                        border: InputBorder.none,
                        hintStyle: TextStyle(
                          fontSize: AdapterUtils.getSp(14),
                          color: Color(0xff606B79),
                          fontWeight: FontWeight.w500,
                        )),
                  ),
                ],
              ),
            ),
            // SizedBox(height: AdapterUtils.getHeight(23)),
            MySeparatorWidget(
              color: Color(0xff3B4859),
            ),
            SizedBox(height: AdapterUtils.getHeight(30)),
            Container(
              height: AdapterUtils.getHeight(50),
              child: Row(
                children: <Widget>[
                  Text('设为默认地址',
                      style: TextStyle(
                          color: Color(0xff99A2AD),
                          fontWeight: FontWeight.w500,
                          fontSize: AdapterUtils.getSp(14))),
                  Spacer(),
                  CupertinoSwitch(
                      value: this.isActive,
                      trackColor: Color(0xff606B79),
                      activeColor: Color(0xffFF3D3E),
                      onChanged: (value) {
                        if (mounted) {
                          setState(() {
                            this.isActive = value;
                          });
                        }
                      })
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }

  void postApi() {
    if (acceptNameController.text.isEmpty) {
      FLToast.showText(text: '请输入收货人');
      return;
    }
    if (phoneController.text.isEmpty) {
      FLToast.showText(text: '请输入手机号码');
      return;
    }
    if (cityID.isEmpty) {
      FLToast.showText(text: '请选择所在地区');
      return;
    }
    if (addressController.text.isEmpty) {
      FLToast.showText(text: '请输入详细地址信息，如道路、门牌号、写字楼座号、单元室等');
      return;
    }
    var dismiss = FLToast.loading(text: '正在保存...');
    Http.instance.post(
        'city/address-modify',
        {
          'acceptName': acceptNameController.text,
          'phone': phoneController.text,
          'city': this.cityID,
          'address': addressController.text,
          'check': isActive ? 1 : 0,
          'id': widget.model.id,
        },
        onSuccess: (data) {
          FLToast.showSuccess(text: '保存成功');
          widget.callBack();
          Navigator.pop(context);
        },
        onError: (error) {},
        onFail: (fail) {},
        complete: () {
          dismiss();
        });
  }

  void bindDelet() {
    showDialog(
        context: context,
        builder: (context) {
          return Material(
            type: MaterialType.transparency,
            child: Center(
                child: Container(
              height: 128,
              width: 280,
              color: Colors.white,
              child: Column(
                mainAxisAlignment: MainAxisAlignment.start,
                children: <Widget>[
                  SizedBox(
                    height: 35,
                  ),
                  Align(
                    child: Text('删除的地址将不会恢复',
                        style: TextStyle(
                            color: Color(0xff141F33),
                            fontWeight: FontWeight.w400,
                            fontSize: AdapterUtils.getSp(16))),
                    alignment: Alignment.center,
                  ),
                  SizedBox(
                    height: 27,
                  ),
                  Container(
                    height: 1,
                    color: Color(0xff191F25).withOpacity(0.08),
                  ),
                  Container(
                    height: 40,
                    child: Row(
                      children: <Widget>[
                        Expanded(
                            child: InkWell(
                          onTap: () {
                            Navigator.pop(context, 0);
                          },
                          child: Container(
                            alignment: Alignment.center,
                            child: Text('取消',
                                style: TextStyle(
                                    color: Color(0xff99A2AD),
                                    fontWeight: FontWeight.w500,
                                    fontSize: AdapterUtils.getSp(17))),
                          ),
                        )),
                        Container(
                          width: 1,
                          color: Color(0xff191F25).withOpacity(0.08),
                        ),
                        Expanded(
                            child: InkWell(
                          onTap: () {
                            Navigator.pop(context, 1);
                          },
                          child: Container(
                            alignment: Alignment.center,
                            child: Text('确认',
                                style: TextStyle(
                                    color: Color(0xffFF3D3E),
                                    fontWeight: FontWeight.w500,
                                    fontSize: AdapterUtils.getSp(17))),
                          ),
                        ))
                      ],
                    ),
                  )
                ],
              ),
            )),
          );
        }).then((value) {
      if (value == 1) {
         var dismiss = FLToast.loading(text: '正在删除...');
     Http.instance.post('city/address-del',{'id':widget.model.id,}, onSuccess: (data) {
      FLToast.showSuccess(text:'删除成功');
      widget.callBack();
      Navigator.pop(context);
    }, onError: (error) {}, onFail: (fail) { },complete: (){
      dismiss();
    });
      }
    });
  }
}
